// pages/crowdfunding/edit/index.js
import loginFn from '../../../utils/login'
import urls from '../../../utils/urls'
//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    course_name: '请选择课程',
    course_id: '',
    start_time: '',
    end_time: '',
    schoolId: '',
    courses: [],
    rows: {},
    showMask: false,
    course_price: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options.id = 2;
    // options.school_id = 142734486;
    if(options.data) {
		  try {
        options = JSON.parse(decodeURIComponent(options.data));
      } catch (error) {
        options = JSON.parse(options.data);
      }
    }

    console.log('options', options);
    if(options.id) {
      wx.setNavigationBarTitle({
        title: '编辑活动',
      })
      this.getInfo(options.id);
    } else {
      var date = new Date();
      var nowDate = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
      var date2 = new Date(date);
      date2.setDate(date.getDate() + 30);
      var endDate = date2.getFullYear()+'-'+(date2.getMonth()+1)+'-'+date2.getDate();

      this.setData({
        start_time: nowDate,
        end_time: endDate,
      });

    }

    this.setData({
      schoolId: options.school_id,
      course_id: options.course_id ? options.course_id : '',
      id: options.id ? options.id : ''
    });

    this.getCourseList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  startTimeChange(e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  endTimeChange(e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  //提交表单
  formSubmit(e) {
    console.log(e);
    var postData = e.detail.value;

    postData.course_id = this.data.course_id;
    if(!postData.title) {
      app.alert('请填写活动标题'); return false;
    }

    if(!postData.course_id) {
      app.alert('请选择课程'); return false;
    }
    if(!postData.number_total) {
      app.alert('请填写拼课人数'); return false;
    }

    if(!postData.min_price) {
      app.alert('请填写最低价格'); return false;
    }

    if(!postData.deposit) {
      app.alert('请填写订金金额'); return false;
    }

    

    postData.id = this.data.id;
    let that = this;
    app.request({
      url: 'crowdfunding/edit',

      data: postData,
      sCallback: (res) => {
        if(res.code == 1) {
          app.alert(res.msg, () => {
            wx.navigateBack();
          });
        } else {
          app.alert(res.msg);
        }
      }
    });

  },
  //获取课程列表
  getCourseList(){
    let that = this;
    app.request({
      url: urls.coursebySchool,
      newApi: false,
      data: {
        schoolId: that.data.schoolId
      },
      sCallback: (res) => {
        if(res.code == 0 && res.data.length > 0) {
          this.setData({
            courses: res.data
          });
        } else {
          app.alert('请先添加课程', () => {
            wx.navigateBack();
          })
        }
      }
    });
  },
  //获取详情
  getInfo(id) {
    let that = this;
    app.request({
      url: 'crowdfunding/detail',
      data: {
        id: id
      },
      sCallback: (res) => {
        if(res.code == 1) {
          this.setData({
            rows: res.data,
            course_name: res.data.courseName,
            start_time: res.data.start_time,
            end_time: res.data.end_time,
            course_id: res.data.course_id,
            course_price: res.data.price
          });
        }
      }
    });
  },
  //显示选择课程
  showCourse() {
    this.setData({
      showMask: true
    });
  },
  hideMask() {
    this.setData({
      showMask: false
    })
  },
  selectCourse(e) {
    let index = app.getBindParam(e, 'index');
    let course = this.data.courses[index];
    this.setData({
      course_id: course.courseId,
      course_name: course.courseName,
      showMask: false,
      course_price: course.price
    });
  }
})